package com.example.redisstudy1.service;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: 杨通通
 * @Date: 2024/10/30 23:04
 * @Description:
 */


@SpringBootTest
public class transaction1 {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Test
    void test1(){
       redisTemplate.execute(new SessionCallback<Object>() {
           @Override
           public  Object execute(RedisOperations operations) throws DataAccessException {
               operations.multi();//开启事务
                //无需try catch，出现异常自动回滚
               operations.opsForValue().set("uname","ytt");
               operations.opsForValue().set("myage",12);
               operations.exec();//提交事务
               return null;
           }
       });

    }


    @Test
    void test2(){
        redisTemplate.execute(new SessionCallback<Object>() {

            @Override
            public  Object execute(RedisOperations operations) throws DataAccessException {
                operations.multi();//开启事务
                //无需try catch，出现异常自动回滚
                operations.opsForValue().set("uname","ytt");
                operations.opsForValue().set("myage",12);
                operations.exec();//提交事务
                return null;
            }
        });
    }

}